package com.sun.alen.utils;

import com.cloopen.rest.sdk.BodyType;
import com.cloopen.rest.sdk.CCPRestSmsSDK;
import com.sun.alen.entity.VerifyCode;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

@Slf4j
public class PhoneUtils {
    private static final Map<String, VerifyCode> codeMap=new HashMap<>();

    public boolean setCode(String phone,String code) {
        //生产环境请求地址：app.cloopen.com
        String serverIp = "app.cloopen.com";
        //请求端口
        String serverPort = "8883";
        //主账号,登陆云通讯网站后,可在控制台首页看到开发者主账号ACCOUNT SID和主账号令牌AUTH TOKEN
        String accountSId = "2c94811c8cd4da0a018eea0060956162";
        String accountToken = "8a3f0ffd493342018594d672d848d302";
        //请使用管理控制台中已创建应用的APPID
        String appId = "2c94811c8cd4da0a018eea00620b6169";
        CCPRestSmsSDK sdk = new CCPRestSmsSDK();
        sdk.init(serverIp, serverPort);
        sdk.setAccount(accountSId, accountToken);
        sdk.setAppId(appId);
        sdk.setBodyType(BodyType.Type_XML);
        //添加你自己的手机号
        String to = "18382736129";
        String templateId = "1";
        String[] datas = {code};
        //  String subAppend="1234";  //可选	扩展码，四位数字 0~9999
        //  String reqId="***";  //可选 第三方自定义消息id，最大支持32位英文数字，同账号下同一自然天内不允许重复
        HashMap<String, Object> result = sdk.sendTemplateSMS(to, templateId, datas);
        //  HashMap<String, Object> result = sdk.sendTemplateSMS(to,templateId,datas,subAppend,reqId);
        if ("000000".equals(result.get("statusCode"))) {
            //正常返回输出data包体信息（map）
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                System.out.println(key + " = " + object);
            }
            return true;
        } else {
            //异常返回输出错误码和错误信息
            System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            return false;
        }
    }

    public  String getCode(){
        // 生成6位随机数字作为验证码
        Random random = new Random();
        int min = 1000;
        int max = 9999;
        int code = random.nextInt(max - min + 1) + min;
        return String.valueOf(code);
    }

    // 发送验证码并保存
    public boolean sendCode(String phone) {
        String code = getCode();
        if (code == null) {
            return false;
        }
        if (setCode(phone, code)) {
            // 保存验证码和发送时间
            codeMap.put(phone, new VerifyCode(code, System.currentTimeMillis()));
            return true;
        }
        return false;
    }

    // 验证验证码是否正确且未过期
    public boolean verifyCode(String phone, String code) {
        VerifyCode verifyCode = codeMap.get(phone);
//        log.info("{}",verifyCode);
//        log.info("{}",phone);
//        log.info("{}",code);
        if (verifyCode != null && verifyCode.getCode().equals(code)) {

            // 验证码存在且相同
            long currentTime = System.currentTimeMillis();
            long sendTime = verifyCode.getSendTime();
            // 验证码有效期为 2 分钟
            long validityPeriod = 2 * 60 * 1000; // 2 分钟转换为毫秒
            return currentTime - sendTime <= validityPeriod;
        }
        return false;
    }
}
